package com.haier.nengyuan.myapplication;

import android.graphics.PointF;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by wy on 2017/8/28.
 */

public class WeekBarChartFragment01 extends BaseFragment {

    private ArrayList<PointF> mPointArrayList = new ArrayList<>();
    private TextView mTvWeekEarningTotal;
    private FrameLayout frameLayout;
    private float mWeekEarningTotal;
    private String mStrHour;
    private String mStrDay;
    private String mStrMonth;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_week_barchart_01, container, false);

        mTvWeekEarningTotal = (TextView) view.findViewById(R.id.tv_week_earning_total);
        frameLayout = (FrameLayout) view.findViewById(R.id.frame_layout);

        return view;
    }

    //    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @Override
//    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        WindowManager wm = getActivity().getWindowManager();
//        int width = (wm.getDefaultDisplay().getWidth() - 140) / 7;
//        Calendar rightNow = Calendar.getInstance();
//        mStrHour = MyDateUtil.changeH(System.currentTimeMillis());
//        mStrDay = MyDateUtil.changeD(System.currentTimeMillis());
//        Integer day = Integer.valueOf(mStrDay);
//        mStrMonth = MyDateUtil.changeM(System.currentTimeMillis());
//
//        int week = rightNow.get(rightNow.DAY_OF_WEEK) - 1;//获取时间/
//
//        if (week == 0)
//            week = 7;
//        //计算本周，今天之前的每一天数据
//
//        if ("09".equals(mStrMonth)) {
//            Log.e("---", "onViewCreated: week:" + week);
//            for (int i = 0; i < week; i++) {
//
//                if (i == week - 1) {
//
//                    mPointArrayList.add(new PointF(points2[i][0], (float) (Float.valueOf(Variable.dayShou))));
//                } else {
//                    mPointArrayList.add(new PointF(points2[i][0], (float) (0.85 * Float.valueOf(Variable.nineDayEnergy[Integer.valueOf(mStrDay) - (week - i)]))));
//                }
//                float y = mPointArrayList.get(i).y;
//
//                Log.e("---", "onViewCreated: " + y);
//
//                mWeekEarningTotal += y;
//
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 34));
//                layoutParams.gravity = Gravity.BOTTOM;
//                layoutParams.setMarginStart((width) * i + 60 + 10);
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//
//        }
//
//        if ("10".equals(mStrMonth)) {
//
//            Integer integer = Integer.valueOf(mStrDay);
//            Log.e("---", "onViewCreated: week:" + week + "  integer===" + integer);
//            for (int i = 0; i < week; i++) {
//
//                if (i == week - 1) {
//                    mPointArrayList.add(new PointF(points2[i][0], (float) (Float.valueOf(Variable.dayShou))));
//                } else {
//
//                    if (integer < week) {
//                        int ia = week - integer;
//                        String sa = Variable.nineDayEnergy[Variable.nineDayEnergy.length - 2 - ia + i + 1];
//                        mPointArrayList.add(new PointF(points2[i][0], (float) (0.85 * Float.valueOf(sa))));
//
//                    } else {
//                        //之前天
//                        String ss = Variable.tenDayEnergy[Integer.valueOf(mStrDay) - (week - i)];
//                        mPointArrayList.add(new PointF(points2[i][0], (float) (0.85 * Float.valueOf(ss))));
//                    }
//                }
//                float y = mPointArrayList.get(i).y;
//
//                Log.e("---", "onViewCreated: " + y);
//
//                mWeekEarningTotal += y;
//
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 34));
//                layoutParams.gravity = Gravity.BOTTOM;
//                layoutParams.setMarginStart((width) * i + 60 + 10);
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//
//        }
//        if ("11".equals(mStrMonth)) {
//            Log.e("---", "onViewCreated: week:" + week);
//            Integer integer = Integer.valueOf(mStrDay);
//            for (int i = 0; i < week; i++) {
//
//                if (i == week - 1) {
//
//                    mPointArrayList.add(new PointF(points2[i][0], (float) (Float.valueOf(Variable.dayShou))));
//                } else {
//                    if (integer < week) {
//                        int ia = week - integer;
//                        String sa = Variable.tenDayEnergy[Variable.nineDayEnergy.length - 2 - ia + i + 1];
//                        mPointArrayList.add(new PointF(points2[i][0], (float) (0.85 * Float.valueOf(sa))));
//
//                    } else {
//                        mPointArrayList.add(new PointF(points2[i][0], (float) (0.85 * Float.valueOf(Variable.eleEnergy[Integer.valueOf(mStrDay) - (week - i)]))));
//                    }
//                }
//                float y = mPointArrayList.get(i).y;
//
//                Log.e("---", "onViewCreated: " + y);
//
//                mWeekEarningTotal += y;
//
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 34));
//                layoutParams.gravity = Gravity.BOTTOM;
//                layoutParams.setMarginStart((width) * i + 60 + 10);
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//
//        }
//
//
//        mTvWeekEarningTotal.setText((float) (Math.round(mWeekEarningTotal * 100)) / 100 + "");
//    }
    private float mDataTotal;
    private float[] dayEnergy;
    private float[] befMonthEnergy;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mDataTotal = 0;
        frameLayout.removeAllViews();

        WindowManager wm = getActivity().getWindowManager();
//        int width = (wm.getDefaultDisplay().getWidth() - 160) / 7;
        int width = (wm.getDefaultDisplay().getWidth() - 140) / 7;

        long time = System.currentTimeMillis();
        int month = Integer.parseInt(MyDateUtil.changeM(time));
        int day = Integer.parseInt(MyDateUtil.changeD(time));
        int hour = Integer.parseInt(MyDateUtil.changeH(time)) / 2;
        float mdaydata = 0;
        for (int i = 0; i <= hour; i++) {
            mdaydata += VirtualData.pointsDay[i][1];
        }
        if (month == 9) {
            dayEnergy = VirtualData.nineDayEnergy;
        } else if (month == 10) {
            dayEnergy = VirtualData.tenDayEnergy;
            befMonthEnergy = VirtualData.nineDayEnergy;
        } else if (month == 11) {
            dayEnergy = VirtualData.eleEnergy;
            befMonthEnergy = VirtualData.tenDayEnergy;
        } else if (month == 12) {
            dayEnergy = VirtualData.decemberEnergy;
            befMonthEnergy = VirtualData.eleEnergy;
        } else if (month == 1) {
            dayEnergy = VirtualData.oneEnergy;
            befMonthEnergy = VirtualData.decemberEnergy;
        }
        Calendar rightNow = Calendar.getInstance();
        int week = rightNow.get(rightNow.DAY_OF_WEEK) - 1;//获取时间
        if (week == 0) {
            week = 7;
        }
        for (int i = 0; i < week; i++) {
            float y = 0;
            if (i == week - 1) {
                y = dayEnergy[day - 1] * (mdaydata / VirtualData.mdataDay);
            } else {
                if (day < week) {
                    //月初
                    int befMonthDay = week - day;
                    if (i < befMonthDay) {
                        y = befMonthEnergy[befMonthEnergy.length - 1 - befMonthDay + i];
                    } else {
                        y = dayEnergy[i - befMonthDay];
                    }//漏掉本月初
//                    y = befMonthEnergy[befMonthEnergy.length - 1 - befMonthDay + i];
                } else {
                    //月末
                    y = dayEnergy[day - week + i];//漏掉下标-1
                }
            }
            mDataTotal += y;

            View view1 = new View(getActivity());
//            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 40, (int) (y * 0.92 * 30));
//            layoutParams.gravity = Gravity.BOTTOM;
//            layoutParams.setMarginStart((width) * i + 60 + 20);

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 34));
            layoutParams.gravity = Gravity.BOTTOM;
            layoutParams.setMarginStart((width) * i + 60 + 10);
            view1.setLayoutParams(layoutParams);
            view1.setBackgroundColor(0xffffffff);

            frameLayout.addView(view1);
        }
        mTvWeekEarningTotal.setText((Math.round(mDataTotal * 0.85 * 100)) / 100f + "");
    }

    @Override
    public int setRootView() {
        return 0;
    }

    @Override
    public void initViews() {

    }
}
